package cn.vihub.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.vihub.param.ShopUserParam;
import cn.vihub.pojo.ShopRole;
import cn.vihub.service.ShopMenuService;
import cn.vihub.service.ShopRoleService;
import cn.vihub.vo.ShopUserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.pojo.ShopUser;
import cn.vihub.service.ShopUserService;
import cn.vihub.mapper.ShopUserMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 64452
* @description 针对表【shop_user(用户数据表)】的数据库操作Service实现
* @createDate 2025-07-17 20:43:24
*/
@Service
public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser>
    implements ShopUserService{

    @Autowired
    private ShopRoleService shopRoleService;

    @Autowired
    private ShopMenuService shopMenuService;

    @Override
    public IPage<ShopUser> selectByPage(ShopUserParam shopUserParam) {

        IPage<ShopUser> iPage = new Page<>(shopUserParam.getPage(), shopUserParam.getLimit());

        LambdaQueryWrapper<ShopUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询用户列表
        lambdaQueryWrapper.like(StringUtils.isNotBlank(shopUserParam.getUsername()),
                ShopUser::getUsername, shopUserParam.getUsername());

        // order by 排序
        // if (StringUtils.isNotBlank(shopUserParam.getPropColumn())){
        //     if ("id".equals(shopUserParam.getPropColumn())){
        //         if ("ascending".equals(shopUserParam.getOrderType())){
        //             lambdaQueryWrapper.orderByAsc(ShopUser::getId);
        //         } else if ("descending".equals(shopUserParam.getOrderType())) {
        //             lambdaQueryWrapper.orderByDesc(ShopUser::getId);
        //         }
        //     }
        // }
        lambdaQueryWrapper.orderBy(StringUtils.isNotBlank(shopUserParam.getPropColumn()),
                "ascending".equals(shopUserParam.getOrderType()), ShopUser::getId);


        return page(iPage, lambdaQueryWrapper);
    }

    /**
     * 添加及编辑用户
     * @param shopUserParam
     * @return
     */
    @Override
    public Boolean save(ShopUserParam shopUserParam) {

        // 创建一个新的对象用来存储用户数据，并且提供给mp的添加及编辑接口
        ShopUser shopUser = new ShopUser();
        // 拷贝
        BeanUtils.copyProperties(shopUserParam, shopUser);

        // 密码加密(md5)
        if (StringUtils.isNotBlank(shopUser.getPassword())){
            String s = SecureUtil.md5(shopUser.getPassword());//hutool加密
            shopUser.setPassword(s);
        }

        return saveOrUpdate(shopUser);

    }

    @Override
    public ShopUserVo getUser(Integer id) {

        // 获取用户详情
        ShopUser shopUser = getById(id);

        // 获取用户的角色列表
        ShopUserParam shopUserParam = new ShopUserParam();
        shopUserParam.setId(id);
        List<ShopRole> roleList = shopRoleService.getRoleListByUserId(shopUserParam);

        // 获取用户的菜单树
        Object menuTree = shopMenuService.treeByUserId(id);

        ShopUserVo shopUserVo = new ShopUserVo();
        shopUserVo.setId(shopUser.getId());
        shopUserVo.setUsername(shopUser.getUsername());
        shopUserVo.setRoleList(roleList);
        shopUserVo.setMenuTree(menuTree);

        return shopUserVo;
    }

    @Override
    public ShopUser getUserByUsername(String username) {

        LambdaQueryWrapper<ShopUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShopUser::getUsername, username);
        return getOne(lambdaQueryWrapper);
    }
}




